package com.amit.servlet;

import java.io.IOException;



import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

@WebServlet(name="jlc",urlPatterns={"*.jlc"},loadOnStartup=1)
public class JLCControllerServlet extends HttpServlet {
	
	
	Logger log=Logger.getLogger(JLCControllerServlet.class);
	LoginAction loginAction=null;
	RegisterAction regAction=null;
	ForgotPWAction fpwAction=null;
	JLCBaseAction action=null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		log.info("Intializing JLCControllerServlet");
		loginAction= new LoginAction();
		regAction =new RegisterAction();
		fpwAction =new  ForgotPWAction();
		
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		log.info("JLCContollerServlet is processing the request");
		
		String URI=request.getRequestURI();
		log.info("Requesting the URI:"+URI);
		if(URI.endsWith("login.jlc"))
		{
			action=loginAction;
			
		}
		else if(URI.endsWith("register.jlc"))
		{
			action=regAction;
		}
		else if(URI.endsWith("forgotpw.jlc"))
		{
			action=fpwAction;
		}
		
		String result=" ";
		
		try
		{
			result=action.service(request,response);
		}
		catch (Exception e) {
			log.error(e);
			
		}
		
		log.info("Resultant Resource:"+result);
		RequestDispatcher rd=request.getRequestDispatcher(result);
		rd.forward(request, response);
		log.info("JLCControllerServlet processing completed");
		
	}

}
